/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package mario.game.graymachine.sprite;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11Ext;

import android.util.Log;

import mario.game.graymachine.core.Renderable;
import mario.game.graymachine.core.Scene;

/**
 * This is the OpenGL ES version of a sprite.  It is more complicated than the
 * CanvasSprite class because it can be used in more than one way.  This class
 * can draw using a grid of verts, a grid of verts stored in VBO objects, or
 * using the DrawTexture extension.
 */
public class GLSprite extends Renderable{
	static final int STATE_MOVE=1;
	static final int STATE_ATTACK=2;
	static final int STATE_UNDER_ATTACK=3;
	static final int STATE_DEAD=4;
	protected int state=STATE_MOVE;
	static final int TYPE_ENIMY=2;
	static final int TYPE_POWERUP=3;
	static final int TYPE_SPRI_BULLET=102;
	static final int TYPE_PLAYER_BULLET=101;
	public static final int TYPE_STONE = 103;
	//sprite type
	public int type;
    // The OpenGL ES texture handle to draw.
    protected int mTextureName;
    // The id of the original resource that mTextureName is based on.
    protected int mResourceId;
    protected int[][] mTextureNames;
    protected int[][] mResourceIds; // states ;  anima of  a state
    protected Scene mScene;
    // If drawing with verts or VBO verts, the grid object defining those verts.
	public boolean isDead;
    
    public GLSprite(Scene scene){
    	mScene=scene;
    }
    public void setup(){
    	isDead=false;
    	x=0;
    	y=0;
    	state=STATE_MOVE;
    }
    public GLSprite(int resourceId) {
        super();
        mResourceId = resourceId;
    }
    
    public void setTextureName(int name) {
        mTextureName = name;
    }
    
    public int getTextureName() {
        return mTextureName;
    }
    
    public void setResourceId(int id) {
        mResourceId = id;
    }
    
    public int getResourceId() {
        return mResourceId;
    }
    
    public int[][] getTextureNames() {
		return mTextureNames;
	}

	public void setTextureNames(int[][] textureNames) {
		mTextureNames = textureNames;
	}

	public int[][] getResourceIds() {
		return mResourceIds;
	}

	public void setResourceIds(int[][] resourceIds) {
		mResourceIds = resourceIds;
	}
    
    public void draw(GL10 gl) {
    	
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName);
        ((GL11Ext) gl).glDrawTexfOES(x, y, z, width, height);
    }
    
    public void update(int timer){
    	detectExceedScreen();
    	move(timer);
    }
    

	protected void move(int timer) {
		x += timer * velocityX/100;
		y += timer * velocityY/100;
		
	}
	protected void detectExceedScreen() {
		if((x<-width)||(x>480)||(y<0)||(y>320+height))
			isDead=true;
		
	}
	public boolean collisionWith(GLSprite something) {
		return false;
	}
}
